﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using Dukey.DBUtility;

namespace BLL
{
    /// <summary>
    /// 版块表
    /// </summary>
    public class Article
    {
        public static readonly Article instance = new Article();

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Article");
            strSql.Append(" where id=@id ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)};
            parameters[0].Value = id;

            return DbHelper.Exists(strSql.ToString(), parameters);
        }


        #region 增加一条数据
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Dukey.Model.Article model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Article(");
            strSql.Append("prefix,title,fulltitle,title2,color,tags,brief,author,source,contents,picurl,bid,isshow,dots,px,level,istj,isfocus,isheader,istop,iscreate,iscomment,addtime,uptime)");
            strSql.Append(" values (");
            strSql.Append("@prefix,@title,@fulltitle,@title2,@color,@tags,@brief,@author,@source,@contents,@picurl,@bid,@isshow,@dots,@px,@level,@istj,@isfocus,@isheader,@istop,@iscreate,@iscomment,@addtime,@uptime)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@prefix", OleDbType.Integer,4),
					new OleDbParameter("@title", OleDbType.VarChar,50),
					new OleDbParameter("@fulltitle", OleDbType.VarChar,50),
					new OleDbParameter("@title2", OleDbType.VarChar,50),
					new OleDbParameter("@color", OleDbType.VarChar,10),
					new OleDbParameter("@tags", OleDbType.VarChar,255),
					new OleDbParameter("@brief", OleDbType.VarChar,255),
					new OleDbParameter("@author", OleDbType.VarChar,30),
					new OleDbParameter("@source", OleDbType.VarChar,30),
					new OleDbParameter("@contents", OleDbType.VarChar,0),
					new OleDbParameter("@picurl", OleDbType.VarChar,250),
					new OleDbParameter("@bid", OleDbType.Integer,4),
					new OleDbParameter("@isshow", OleDbType.Boolean,1),
					new OleDbParameter("@dots", OleDbType.Integer,4),
					new OleDbParameter("@px", OleDbType.Integer,4),
					new OleDbParameter("@level", OleDbType.Integer,4),
					new OleDbParameter("@istj", OleDbType.Boolean,1),
					new OleDbParameter("@isfocus", OleDbType.Boolean,1),
					new OleDbParameter("@isheader", OleDbType.Boolean,1),
					new OleDbParameter("@istop", OleDbType.Boolean,1),
					new OleDbParameter("@iscreate", OleDbType.Boolean,1),
					new OleDbParameter("@iscomment", OleDbType.Boolean,1),
					new OleDbParameter("@addtime", OleDbType.Date),
					new OleDbParameter("@uptime", OleDbType.Date)};
            parameters[0].Value = model.prefix;
            parameters[1].Value = model.title;
            parameters[2].Value = model.fulltitle;
            parameters[3].Value = model.title2;
            parameters[4].Value = model.color;
            parameters[5].Value = model.tags;
            parameters[6].Value = model.brief;
            parameters[7].Value = model.author;
            parameters[8].Value = model.source;
            parameters[9].Value = model.contents;
            parameters[10].Value = model.picurl;
            parameters[11].Value = model.bid;
            parameters[12].Value = model.isshow;
            parameters[13].Value = model.dots;
            parameters[14].Value = model.px;
            parameters[15].Value = model.level;
            parameters[16].Value = model.istj;
            parameters[17].Value = model.isfocus;
            parameters[18].Value = model.isheader;
            parameters[19].Value = model.istop;
            parameters[20].Value = model.iscreate;
            parameters[21].Value = model.iscomment;
            parameters[22].Value = model.addtime;
            parameters[23].Value = model.uptime;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        #endregion

        #region 更新一条数据
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Dukey.Model.Article model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Article set ");
            strSql.Append("prefix=@prefix,");
            strSql.Append("title=@title,");
            strSql.Append("fulltitle=@fulltitle,");
            strSql.Append("title2=@title2,");
            strSql.Append("color=@color,");
            strSql.Append("tags=@tags,");
            strSql.Append("brief=@brief,");
            strSql.Append("author=@author,");
            strSql.Append("source=@source,");
            strSql.Append("contents=@contents,");
            strSql.Append("picurl=@picurl,");
            strSql.Append("bid=@bid,");
            strSql.Append("isshow=@isshow,");
            strSql.Append("dots=@dots,");
            strSql.Append("px=@px,");
            strSql.Append("level=@level,");
            strSql.Append("istj=@istj,");
            strSql.Append("isfocus=@isfocus,");
            strSql.Append("isheader=@isheader,");
            strSql.Append("istop=@istop,");
            strSql.Append("iscreate=@iscreate,");
            strSql.Append("iscomment=@iscomment,");
            strSql.Append("addtime=@addtime,");
            strSql.Append("uptime=@uptime");
            strSql.Append(" where id=@id ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@prefix", OleDbType.Integer,4),
					new OleDbParameter("@title", OleDbType.VarChar,50),
					new OleDbParameter("@fulltitle", OleDbType.VarChar,50),
					new OleDbParameter("@title2", OleDbType.VarChar,50),
					new OleDbParameter("@color", OleDbType.VarChar,10),
					new OleDbParameter("@tags", OleDbType.VarChar,255),
					new OleDbParameter("@brief", OleDbType.VarChar,255),
					new OleDbParameter("@author", OleDbType.VarChar,30),
					new OleDbParameter("@source", OleDbType.VarChar,30),
					new OleDbParameter("@contents", OleDbType.VarChar,0),
					new OleDbParameter("@picurl", OleDbType.VarChar,250),
					new OleDbParameter("@bid", OleDbType.Integer,4),
					new OleDbParameter("@isshow", OleDbType.Boolean,1),
					new OleDbParameter("@dots", OleDbType.Integer,4),
					new OleDbParameter("@px", OleDbType.Integer,4),
					new OleDbParameter("@level", OleDbType.Integer,4),
					new OleDbParameter("@istj", OleDbType.Boolean,1),
					new OleDbParameter("@isfocus", OleDbType.Boolean,1),
					new OleDbParameter("@isheader", OleDbType.Boolean,1),
					new OleDbParameter("@istop", OleDbType.Boolean,1),
					new OleDbParameter("@iscreate", OleDbType.Boolean,1),
					new OleDbParameter("@iscomment", OleDbType.Boolean,1),
					new OleDbParameter("@addtime", OleDbType.Date),
					new OleDbParameter("@uptime", OleDbType.Date)};
            parameters[0].Value = model.prefix;
            parameters[1].Value = model.title;
            parameters[2].Value = model.fulltitle;
            parameters[3].Value = model.title2;
            parameters[4].Value = model.color;
            parameters[5].Value = model.tags;
            parameters[6].Value = model.brief;
            parameters[7].Value = model.author;
            parameters[8].Value = model.source;
            parameters[9].Value = model.contents;
            parameters[10].Value = model.picurl;
            parameters[11].Value = model.bid;
            parameters[12].Value = model.isshow;
            parameters[13].Value = model.dots;
            parameters[14].Value = model.px;
            parameters[15].Value = model.level;
            parameters[16].Value = model.istj;
            parameters[17].Value = model.isfocus;
            parameters[18].Value = model.isheader;
            parameters[19].Value = model.istop;
            parameters[20].Value = model.iscreate;
            parameters[21].Value = model.iscomment;
            parameters[22].Value = model.addtime;
            parameters[23].Value = model.uptime;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        #endregion

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Article ");
            strSql.Append(" where id=@id ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)};
            parameters[0].Value = id;

            DbHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        #region 得到一个对象实体
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Dukey.Model.Article GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select prefix,title,fulltitle,title2,color,tags,brief,author,source,contents,picurl,bid,isshow,dots,px,[level],istj,isfocus,isheader,istop,iscreate,iscomment,addtime,uptime from Article ");
            strSql.Append(" where id=@id ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@id", OleDbType.Integer,4)};
            parameters[0].Value = id;

            Dukey.Model.Article model = new Dukey.Model.Article();
            OleDbDataReader dr = DbHelper.ExecuteReader(strSql.ToString(), parameters);
            if (dr.Read())
            {

                if (dr["prefix"].ToString() != "")
                {
                    model.prefix = int.Parse(dr["prefix"].ToString());
                }
                model.title = dr["title"].ToString();
                model.fulltitle = dr["fulltitle"].ToString();
                model.title2 = dr["title2"].ToString();
                model.color = dr["color"].ToString();
                model.tags = dr["tags"].ToString();
                model.brief = dr["brief"].ToString();
                model.author = dr["author"].ToString();
                model.source = dr["source"].ToString();
                model.contents = dr["contents"].ToString();
                model.picurl = dr["picurl"].ToString();
                if (dr["bid"].ToString() != "")
                {
                    model.bid = int.Parse(dr["bid"].ToString());
                }
                if (dr["isshow"].ToString() != "")
                {
                    if ((dr["isshow"].ToString() == "1") || (dr["isshow"].ToString().ToLower() == "true"))
                    {
                        model.isshow = true;
                    }
                    else
                    {
                        model.isshow = false;
                    }
                }
                if (dr["dots"].ToString() != "")
                {
                    model.dots = int.Parse(dr["dots"].ToString());
                }
                if (dr["px"].ToString() != "")
                {
                    model.px = int.Parse(dr["px"].ToString());
                }
                if (dr["level"].ToString() != "")
                {
                    model.level = int.Parse(dr["level"].ToString());
                }
                if (dr["istj"].ToString() != "")
                {
                    if ((dr["istj"].ToString() == "1") || (dr["istj"].ToString().ToLower() == "true"))
                    {
                        model.istj = true;
                    }
                    else
                    {
                        model.istj = false;
                    }
                }
                if (dr["isfocus"].ToString() != "")
                {
                    if ((dr["isfocus"].ToString() == "1") || (dr["isfocus"].ToString().ToLower() == "true"))
                    {
                        model.isfocus = true;
                    }
                    else
                    {
                        model.isfocus = false;
                    }
                }
                if (dr["isheader"].ToString() != "")
                {
                    if ((dr["isheader"].ToString() == "1") || (dr["isheader"].ToString().ToLower() == "true"))
                    {
                        model.isheader = true;
                    }
                    else
                    {
                        model.isheader = false;
                    }
                }
                if (dr["istop"].ToString() != "")
                {
                    if ((dr["istop"].ToString() == "1") || (dr["istop"].ToString().ToLower() == "true"))
                    {
                        model.istop = true;
                    }
                    else
                    {
                        model.istop = false;
                    }
                }
                if (dr["iscreate"].ToString() != "")
                {
                    if ((dr["iscreate"].ToString() == "1") || (dr["iscreate"].ToString().ToLower() == "true"))
                    {
                        model.iscreate = true;
                    }
                    else
                    {
                        model.iscreate = false;
                    }
                }
                if (dr["iscomment"].ToString() != "")
                {
                    if ((dr["iscomment"].ToString() == "1") || (dr["iscomment"].ToString().ToLower() == "true"))
                    {
                        model.iscomment = true;
                    }
                    else
                    {
                        model.iscomment = false;
                    }
                }
                if (dr["addtime"].ToString() != "")
                {
                    model.addtime = DateTime.Parse(dr["addtime"].ToString());
                }
                if (dr["uptime"].ToString() != "")
                {
                    model.uptime = DateTime.Parse(dr["uptime"].ToString());
                }
                dr.Dispose();
                dr.Close();
                return model;
            }
            else
            {
                dr.Dispose();
                dr.Close();
                return null;
            }
        }
        #endregion

        public List<Dukey.Model.Article> GetListByCache(string strWhere)
        {
            object objModel = Common.DataCache.GetCache("articlelist");
            if (objModel == null)
            {
                try
                {
                    objModel = GetList(strWhere);
                    if (objModel != null)
                    {
                        Common.DataCache.SetCache("articlelist", objModel);
                    }
                }
                catch { }
            }
            return objModel as List<Dukey.Model.Article>;           
        }

        #region 获得数据列表
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Dukey.Model.Article> GetList(string strWhere)
        {
            List<Dukey.Model.Article> list = new List<Dukey.Model.Article>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [id],prefix,title,fulltitle,title2,color,tags,brief,author,source,picurl,bid,isshow,dots,px,[level],istj,isfocus,isheader,istop,iscreate,iscomment,addtime,uptime ");
            strSql.Append(" FROM Article ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            OleDbDataReader dr = DbHelper.ExecuteReader(strSql.ToString());
            while (dr.Read())
            {
                Dukey.Model.Article model = new Dukey.Model.Article();
                if (dr["id"].ToString() != "")
                {
                    model.id = int.Parse(dr["id"].ToString());
                }
                if (dr["prefix"].ToString() != "")
                {
                    model.prefix = int.Parse(dr["prefix"].ToString());
                }
                model.title = dr["title"].ToString();
                model.fulltitle = dr["fulltitle"].ToString();
                model.title2 = dr["title2"].ToString();
                model.color = dr["color"].ToString();
                model.tags = dr["tags"].ToString();
                model.brief = dr["brief"].ToString();
                model.author = dr["author"].ToString();
                model.source = dr["source"].ToString();
                model.picurl = dr["picurl"].ToString();
                if (dr["bid"].ToString() != "")
                {
                    model.bid = int.Parse(dr["bid"].ToString());
                }
                if (dr["isshow"].ToString() != "")
                {
                    if ((dr["isshow"].ToString() == "1") || (dr["isshow"].ToString().ToLower() == "true"))
                    {
                        model.isshow = true;
                    }
                    else
                    {
                        model.isshow = false;
                    }
                }
                if (dr["dots"].ToString() != "")
                {
                    model.dots = int.Parse(dr["dots"].ToString());
                }
                if (dr["px"].ToString() != "")
                {
                    model.px = int.Parse(dr["px"].ToString());
                }
                if (dr["level"].ToString() != "")
                {
                    model.level = int.Parse(dr["level"].ToString());
                }
                if (dr["istj"].ToString() != "")
                {
                    if ((dr["istj"].ToString() == "1") || (dr["istj"].ToString().ToLower() == "true"))
                    {
                        model.istj = true;
                    }
                    else
                    {
                        model.istj = false;
                    }
                }
                if (dr["isfocus"].ToString() != "")
                {
                    if ((dr["isfocus"].ToString() == "1") || (dr["isfocus"].ToString().ToLower() == "true"))
                    {
                        model.isfocus = true;
                    }
                    else
                    {
                        model.isfocus = false;
                    }
                }
                if (dr["isheader"].ToString() != "")
                {
                    if ((dr["isheader"].ToString() == "1") || (dr["isheader"].ToString().ToLower() == "true"))
                    {
                        model.isheader = true;
                    }
                    else
                    {
                        model.isheader = false;
                    }
                }
                if (dr["istop"].ToString() != "")
                {
                    if ((dr["istop"].ToString() == "1") || (dr["istop"].ToString().ToLower() == "true"))
                    {
                        model.istop = true;
                    }
                    else
                    {
                        model.istop = false;
                    }
                }
                if (dr["iscreate"].ToString() != "")
                {
                    if ((dr["iscreate"].ToString() == "1") || (dr["iscreate"].ToString().ToLower() == "true"))
                    {
                        model.iscreate = true;
                    }
                    else
                    {
                        model.iscreate = false;
                    }
                }
                if (dr["iscomment"].ToString() != "")
                {
                    if ((dr["iscomment"].ToString() == "1") || (dr["iscomment"].ToString().ToLower() == "true"))
                    {
                        model.iscomment = true;
                    }
                    else
                    {
                        model.iscomment = false;
                    }
                }
                if (dr["addtime"].ToString() != "")
                {
                    model.addtime = DateTime.Parse(dr["addtime"].ToString());
                }
                if (dr["uptime"].ToString() != "")
                {
                    model.uptime = DateTime.Parse(dr["uptime"].ToString());
                }
                list.Add(model);
            }
            dr.Dispose();
            return list;
        }

        #endregion
    }
}
